//
//  FFPayOrderChooseShippingWayView.m
//  FFMall
//
//  Created by ale tan on 2017/9/22.
//  Copyright © 2017年 IT.Bear. All rights reserved.
//

#import "FFPayOrderChooseShippingWayView.h"
#import "FFPayOrderShippingWayCell.h"

@interface FFPayOrderChooseShippingWayView ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic ,strong) NSArray *dataSources;
@property (nonatomic ,strong) FBShippingWayModel *chooseWay;

@property (nonatomic ,strong) ChooseShippingWay chooseShippingWayBlock;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *toBottom;
@end

@implementation FFPayOrderChooseShippingWayView

+ (instancetype)chooseShippingWayWithWays:(NSArray *)shippingWays SelectedWay:(FBShippingWayModel *)selectedWay{
    FFPayOrderChooseShippingWayView *orderView = [[[NSBundle mainBundle] loadNibNamed:@"FFPayOrderChooseShippingWayView" owner:nil options:nil] firstObject];
    orderView.dataSources = shippingWays;
    orderView.chooseWay = selectedWay;
    return orderView;
}

- (void)show{
    UIView *view = [UIApplication sharedApplication].keyWindow.rootViewController.view;//[[UIApplication sharedApplication].delegate window];
    self.frame = view.bounds;
    [self layoutIfNeeded];
    [view addSubview:self];
}

- (void)setChooseShippingWay:(ChooseShippingWay)chooseShippingWay{
    _chooseShippingWayBlock = chooseShippingWay;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    
    _toBottom.constant = isDeviceIphoneX?34:0;
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark -UITableView-
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSources.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    FBShippingWayModel *model = _dataSources[indexPath.row];
    return [FFPayOrderShippingWayCell calculateForHeightWithString:model.shippingDescription];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FFPayOrderShippingWayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FFPayOrderShippingWayCell"];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"FFPayOrderShippingWayCell" owner:nil options:nil] firstObject];
    }
    
    FBShippingWayModel *model = _dataSources[indexPath.row];
    if ([model.shippingId isEqual:_chooseWay.shippingId]) {
        [cell setSelected:YES];
        [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
    else{
        [cell setSelected:NO];
    }
    [cell updateCellWithModel:model IndexPath:indexPath];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    _chooseWay = _dataSources[indexPath.row];
}

#pragma mark -Events-
- (IBAction)clickBackgroundToCancle:(id)sender {
    [self removeFromSuperview];
}

- (IBAction)clickToCancleChooseWay:(id)sender {
    [self removeFromSuperview];
    if (_chooseShippingWayBlock) {
        _chooseShippingWayBlock(nil);
    }
}

- (IBAction)clickToConfirmChooseWay:(id)sender {
    [self removeFromSuperview];
    if (_chooseShippingWayBlock) {
        _chooseShippingWayBlock(_chooseWay);
    }
}
@end
